Functions

A function is a collection of statements that perform a certain task. One can divide up the code into separate functions, keeping in mind that each function must perform a specific task. Functions are used to put some common and repeated task into a single function, so instead of writing the same code again and again for different inputs, we can simply call the function. Scala is assumed as functional programming language so these play an important role. It makes easier to debug and modify the code. Scala functions are first class values.

Difference between Scala Functions & Methods: Function is a object which can be stored in a variable. But a method always belongs to a class which has a name, signature bytecode etc. Basically, you can say a method is a function which is a member of some object.

Type of Functions
  • Pure Functions
  • Impure Functions
Pure Function:- Pure functions evaluates to same result for a given set of inputs.Period!
Impure Function:- Impure Function do not evaluate to the same output for the given set of inputs.


In general, function declaration & definition have 6 components:
  • def keyword: “def” keyword is used to declare a function in Scala.
  • function_name: It should be valid name in lower camel case. Function name in Scala can have characters like +, ~, &, –, ++, \, / etc.
  • parameter_list: In Scala, comma-separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis.
  • return_type: User must mention return type of parameters while defining function and return type of a function is optional. If you don’t specify any return type of a function, default return type is Unit which is equivalent to void in Java.
  • = : In Scala, a user can create function with or without = (equal) operator. If the user uses it, the function will return the desired value. If he doesn’t use it, the function will not return any value and will work like a subroutine.
  • Method body: Method body is enclosed between braces { }. The code you need to be executed to perform your intended operations.
Syntax:
def function_name ([parameter_list]) : [return_type] = {
    // function body
}

You can create function with or without = (equal) operator. If you use it, function will return value. If you don't use it, your function will not return anything and will work like subroutine. In scala return statement is optional if you not use return statement by default compiler returns the  last expression or statement present in the function.

Note: If the user will not use the equals sign and body then implicitly method is declared abstract.
object demo {
def sum(a: Int, b:Int ):Int = { // method declared
var result= a + b
return result
}
def main(args: Array[String])
{
println( sum(10,20)) //Method call
}
}
OutPut: 30

Scala Function Example without using = Operator
object demo {
def functionExample() {
println("This is a simple function")
}
def main(args: Array[String]) {
functionExample()
}
}
OutPut:This is a simple function

Scala Function Example with = Operator
object demo {
def functionExample() : Int = {
var a = 10
a
}
def main(args: Array[String]) {
var result = functionExample()
println(result)
}
}
OutPut: 10

Method with parameters
object demo {
def main(args: Array[String]) = {
sum(a=10,b=20)
}
def sum (a:Int, b:Int) :Unit = {
var c = a+b
println(c)
}
}
30
object demo {
def add(a:Int,b:Int):Int = {
a+b
}
def sub(a:Int,b:Int):Int = {
a-b
}
def mul(a:Int,b:Int):Int = {
a*b
}
def div(a:Int,b:Int):Int = {
a/b
}
def main(args: Array[String]) = {
var result1 = add(15,2)
var result2 = sub(15,2)
var result3 = mul(15,2)
var result4 = div(15,2)
println(result1+"\n"+result2+"\n"+result3+"\n"+result4 )
}
}


object demo {
def add(a:Int,b:Int) = a+b
def sub(a:Int,b:Int) = a-b
def mul(a:Int,b:Int) = a*b
def div(a:Int,b:Int) = a/b
def main(args: Array[String]) = {
var result1 = add(15,2)
var result2 = sub(15,2)
var result3 = mul(15,2)
var result4 = div(15,2)
println(result1+"\n"+result2+"\n"+result3+"\n"+result4 )
}
}

Function Calling
There are mainly two ways to call the function in Scala. First way is the standard way as follows:
  • function_name(paramter_list)
In the Second way, a user can also call the function with the help of the instance and dot notation as follows:
  • [instance].function_name(paramter_list)

No comments:

Post a Comment